﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Restaurant.Modules.BIZZ.Utility;
using Sector7.Restaurant.Modules.BIZZ.LocalService;
using Sector7.Restaurant.Modules.BIZZ.Controls;
using Sector7.Restaurant.CoreType.Enums;
using Sector7.Restaurant.CoreType.Entity;
using Sector7.Framework.Enums;
using Sector7.Restaurant.Biz;

namespace Sector7.Restaurant.Modules.BIZZ.Windows
{
    /// <summary>
    /// CashiersWindow.xaml 的交互逻辑
    /// </summary>
    public partial class CashiersWindow : UserControl
    {
        private TableStatusInfor _tableStatus = new TableStatusInfor();
        private List<RS_Table> _tables = null;

        public CashiersWindow()
        {
            InitializeComponent();
        }

        private void LoadData()
        {
            _tables = Aide.LocalClient.Invoke<List<RS_Table>>(
                service => service.GetAllTables());

            foreach (RS_Table table in _tables)
            {
                TableControl tableControl = new TableControl(table);
                tableControl.MouseDown += new MouseButtonEventHandler(TableControl_MouseDown);

                wrpMain.Children.Add(tableControl);

                switch (tableControl.Status)
                {
                    case TableStatusType.Idle:
                        ++_tableStatus.Idle;
                        break;
                    case TableStatusType.Used:
                        ++_tableStatus.Used;
                        break;
                    case TableStatusType.Reserved:
                        ++_tableStatus.Reserved;
                        break;
                    case TableStatusType.Disabled:
                        ++_tableStatus.Disabled;
                        break;
                    default:
                        break;
                }

                ++_tableStatus.Total;
            }

            wrpStatus.DataContext = _tableStatus;
        }

        private void TableControl_MouseDown(object sender, MouseButtonEventArgs e)
        {
            TableControl tableControl = sender as TableControl;

            bool? result;
            switch (tableControl.Status)
            {
                case TableStatusType.Idle:
                    SalesWindow salesWindow = new SalesWindow(tableControl.Table);
                    result = salesWindow.ShowDialog();

                    if (result == true)
                    {
                        tableControl.Status = TableStatusType.Used;
                        ++_tableStatus.Used;
                        --_tableStatus.Idle;
                    }
                    break;
                case TableStatusType.Used:
                    PaymentWindow paymentWindow = new PaymentWindow(tableControl.Table);
                    result = paymentWindow.ShowDialog();

                    if (result == true)
                    {
                        tableControl.Status = TableStatusType.Idle;
                        ++_tableStatus.Idle;
                        --_tableStatus.Used;
                    }
                    break;
                default:
                    break;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoadData();
        }

        private void btnPayment_Click(object sender, RoutedEventArgs e)
        {
        }
    }
}
